Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Jump Statements

Continue in Java

Continue

In Java, the continue statement is a control statement that is used to alter the flow of a loop (such as for, while, or do-while) without exiting the loop entirely. It allows you to skip the current iteration of the loop and move to the next iteration, based on a certain condition. The continue statement is particularly useful when you want to selectively skip some loop iterations while continuing with the remaining iterations. Here's a detailed explanation of the continue statement in Java:

Skipping the Current Iteration:

When a continue statement is encountered within a loop, it immediately stops the current iteration of the loop and jumps to the next iteration. This means that any code after the continue statement within the current iteration is skipped.
Continue example in java
public class Main{ public static void main(String[] args) { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skip iteration when i is 3 } System.out.println("Iteration " + i); } } }

Output

Iteration 1 Iteration 2 Iteration 4 Iteration 5
In this example, when i equals 3, the continue statement causes the loop to skip the print statement for that iteration, so the output will be "Iteration 1," "Iteration 2," "Iteration 4," and "Iteration 5."

Conditional Skipping:

The continue statement is often used in conjunction with conditional statements to control which iterations are skipped. For example, you can use it to skip iterations based on certain conditions:
Continue in for loop example in java
public class Main{ public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; // Skip even numbers } System.out.println("Odd number: " + i); } } }

Output

Odd number: 1 Odd number: 3 Odd number: 5 Odd number: 7 Odd number: 9
In this case, the loop skips iterations when i is even, resulting in the printing of odd numbers only.

Continue in Nested Loops :

The continue statement can be used within nested loops to skip the current iteration of the inner loop while continuing with the outer loop. This allows you to fine-tune the behavior of nested loops.
Nested for loops example in java
public class Main{ public static void main(String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i == j) { continue; // Skip when i equals j } System.out.println("i: " + i + ", j: " + j); } } } }

Output

i: 1, j: 2 i: 1, j: 3 i: 2, j: 1 i: 2, j: 3 i: 3, j: 1 i: 3, j: 2
In this example, when i equals j, the continue statement skips the print statement within the inner loop for that specific iteration.

Avoiding Unnecessary Processing:

The continue statement can be used to optimize code by avoiding unnecessary processing. For example, you can skip lengthy calculations or operations based on certain conditions.
Using continue for iteration in java
public class Main{ public static void main(String[] args) { for (int i = 1; i <= 100; i++) { if (i % 10 != 0) { continue; // Skip iterations not divisible by 10 } // Lengthy operation System.out.println("Processed iteration: " + i); } } }

Output

Processed iteration: 10 Processed iteration: 20 Processed iteration: 30 Processed iteration: 40 Processed iteration: 50 Processed iteration: 60 Processed iteration: 70 Processed iteration: 80 Processed iteration: 90 Processed iteration: 100
In this case, the program skips iterations that are not divisible by 10, potentially saving processing time. In summary, the continue statement in Java is a valuable control statement that allows you to skip the current iteration of a loop and move to the next iteration. It is commonly used to selectively execute code within loops based on specific conditions, improving code efficiency and readability.

  📌TAGS

★Continue ★jump ★break statement ★control statement ★control in java ★continue in java

Tutorials